package com.pengcheng.hnweb.util;

import com.pengcheng.hnweb.service.DbService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;

import java.util.*;

@Component
public class ControllerUtil {
    @Autowired
    private Common common;
    @Autowired
    private MenuUtil menuUtil;
    @Autowired
    private DbService dbService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Value("${title}")
    private String title;

    public Model pre(HttpServletRequest request, Model model)
    {
        try {
            model.addAttribute("title",title);
            model.addAttribute("url",request.getRequestURI());
            if(request.getSession().getAttribute("user_id")!=null)
            {
                Object id=request.getSession().getAttribute("user_id");
                model.addAttribute("user_id",id);
                //获取menu
                String sql="select * from hnww_menu where deleted=0 and pid=0 order by fsort asc";
                List<Map<String,Object>> rows_menu=jdbcTemplate.queryForList(sql);
                rows_menu=dbService.get_menu_detail(rows_menu);
                //获取row_user
                sql="select * from hnww_user where id="+id;
                List<Map<String,Object>> rows_user=jdbcTemplate.queryForList(sql);
                if(rows_user!=null&&rows_user.size()>0)
                {
                    model.addAttribute("row_user",rows_user.get(0));
                    int role_id=Integer.parseInt(rows_user.get(0).get("role_id")+"");
                    sql="select * from hnww_roledetail where role_id="+role_id;
                    List<Map<String,Object>> rows_roledetail=jdbcTemplate.queryForList(sql);
                    //根据rows_menu和rows_roledetail更新rows_menu
                    rows_menu=menuUtil.get_power_menu(rows_menu,rows_roledetail);
                    model.addAttribute("rows_menu",rows_menu);
                }
                else {
                    model.addAttribute("rows_menu",new ArrayList<>());
                }
                sql="select * from hnww_menu where deleted=0";
                List<Map<String,Object>> rows_menu2=jdbcTemplate.queryForList(sql);
                Map<String,Object> row_menu=menuUtil.get_row_menu(menuUtil.get_url(request), rows_menu2);
                if(row_menu!=null)
                {
                    model.addAttribute("row_menu",row_menu);
                    model.addAttribute("menu_id",row_menu.get("id"));
                    //menu的路径
                    List<Map<String,Object>> list_menu=menuUtil.get_menu_path(row_menu,rows_menu2);
                    model.addAttribute("list_menu",list_menu);
                }
                else {
                    model.addAttribute("menu_id",0);
                }
            }
            else
            {
                model.addAttribute("user_id",0);
            }
            //获取参数
            Map<String,String> params=new HashMap<>();
            Map<String,String[]> map=request.getParameterMap();
            for(String key:map.keySet())
            {
                params.put(key,common.join(map.get(key)));
            }
            model.addAttribute("json_param",common.tojson(params));
            //配置信息
//        model.addAttribute("row_config",dbService.get_config());
            return model;
        }
        catch (Exception ex)
        {
            return model;
        }
    }

    public Map<String,Object> get_user_by_token(String token)
    {
        String sql="select * from hnww_admin where md5(concat(id,':',username,':',password))=?";
        List<Map<String,Object>> rows_user=jdbcTemplate.queryForList(sql,new Object[]{token});
        if(rows_user==null||rows_user.size()==0)
        {
            return null;
        }
        else {
            return rows_user.get(0);
        }
    }
    public int get_id_fordlog()
    {
        String sql="";
        try {
            List<Map<String,Object>> column_dlog=dbService.columns("dlog");
            Map<String,Object> row_dlog=new HashMap<>();
            row_dlog.put("device_id",-1);
            row_dlog.put("ddate",common.format_date(new Date()));
            row_dlog.put("fdate",common.format_date(new Date()));
            row_dlog.put("deleted",0);
            int id=dbService.save(column_dlog,"dlog",row_dlog);
            return id;
        }
        catch (Exception ex)
        {
            return -1;
        }
    }
}
